package com.weishi.project.training.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weishi.common.enums.RedisConstants;
import com.weishi.common.utils.StringUtils;
import com.weishi.framework.redis.RedisCache;
import com.weishi.project.api.vo.*;
import com.weishi.project.training.domain.LeaveForm;
import com.weishi.project.training.domain.Sportsman;
import com.weishi.project.training.domain.SportsmanBodyShape;
import com.weishi.project.training.domain.SportsmanPhysicalQuality;
import com.weishi.project.training.mapper.SportsmanMapper;
import com.weishi.project.training.mapper.SportsmanPhysicalQualityMapper;
import com.weishi.project.training.service.ILeaveFormService;
import com.weishi.project.training.service.ISportsmanBodyShapeService;
import com.weishi.project.training.service.ISportsmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * sportsmanService业务层处理
 */
@Service
public class SportsmanServiceImpl extends ServiceImpl<SportsmanMapper, Sportsman> implements ISportsmanService {

    @Autowired
    private SportsmanMapper sportsmanMapper;

    @Autowired
    private SportsmanPhysicalQualityMapper sportsmanPhysicalQualityMapper;

    @Autowired
    private ILeaveFormService leaveFormService;

    @Autowired
    private ISportsmanService sportsmanService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 身体形态 接口
     */
    @Autowired
    private ISportsmanBodyShapeService sportsmanBodyShapeService;

    /**
     * 查询sportsman列表
     */
    @Override
    public List<Sportsman> selectSportsmanList(Sportsman sportsman){
        List<Sportsman> list = sportsmanMapper.selectSportsmanList(sportsman);

        try {
            for (Sportsman temp : list) {
                if (leaveFormService.countLeaveFormById(temp.getId())) {
                    LeaveForm leaveForm = leaveFormService.queryBySportsmanId(temp.getId());
                    if(leaveForm.getCureTime()==null || leaveForm.getCureTime().after(new Date())){
                        temp.setStatus(leaveForm.getType());
                    }else{
                        temp.setStatus("0");
                    }
                    sportsmanService.updateById(temp);
                }
                else{
                    temp.setStatus("0");
                    sportsmanService.updateById(temp);
                }
            }
        }
        catch (NullPointerException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Sportsman qrySportsmanByCache(int sportsmanId) {
       /* Sportsman sportsman = redisCache.getCacheObject(RedisConstants.Sportsman_Info_Prefix + sportsmanId);
        if(sportsman==null){
            sportsman = sportsmanMapper.selectById(sportsmanId);
        }*/


        return sportsmanMapper.selectById(sportsmanId);
    }

    @Override
    @Transactional
    public void saveAll(SportsmanAllVo sportsmanAllVo) {
        Sportsman sportsman = sportsmanAllVo.getSportsman();
        SportsmanPhysicalQuality sportsmanPhysicalQuality = sportsmanAllVo.getSportsmanPhysicalQuality();
        sportsman.setCtime(new Date());
        sportsmanMapper.insert(sportsman);
        sportsmanPhysicalQuality.setSportsmanId(sportsman.getId());
        sportsmanPhysicalQualityMapper.insert(sportsmanPhysicalQuality);

        // 新增身体形态
        SportsmanBodyShape sportsmanBodyShape = new SportsmanBodyShape();
        StringUtils.copyProperties(sportsman , sportsmanBodyShape);
        sportsmanBodyShape.setSportsmanId(Long.parseLong(sportsman.getId().toString()));
        sportsmanBodyShapeService.save(sportsmanBodyShape);
    }


    @Override
    public List<Map<String, Integer>> getCountBoatFrequency(BoatFrequencyVo boatFrequencyVo) {
        return sportsmanMapper.getCountBoatFrequency(boatFrequencyVo);
    }

    @Override
    public List<Map<String, Object>> getCountPositionRatio(PositionRatioVo positionRatioVo) {
        return sportsmanMapper.getCountPositionRatio(positionRatioVo);
    }

    @Override
    public List<TrainingBoatTypeVo> getCountTrainingBoatType(Integer sportsManId) {
        return sportsmanMapper.getCountTrainingBoatType(sportsManId);
    }


    @Override
    public List<BodyFunctionAnalysisVo> getBodyFunctionAnalysisVoByweek(int sportsManId) {
        return sportsmanMapper.getBodyFunctionAnalysisVoByweek(sportsManId);
    }

    @Override
    public List<BodyFunctionAnalysisVo> getBodyFunctionAnalysisVoList(int sportsManId) {
        return sportsmanMapper.getBodyFunctionAnalysisVoList(sportsManId);
    }

    @Override
    public List<BestPartnerResultVo> getBestResultSpecialProject(SportManBestResultVo sportManBestResultVo) {
        return sportsmanMapper.getBestResultSpecialProject(sportManBestResultVo);
    }

    @Override
    public List<BestPartnerResultVo> getBestList(Integer sportsManId, Integer  kilometers, String boatType) {
        List<BestPartnerResultVo> list = sportsmanMapper.getBestList(sportsManId, kilometers, boatType);
        Map<String,BestPartnerResultVo> temp = new HashMap<>();
        for(BestPartnerResultVo bestPartnerResultVo : list){
            String key = bestPartnerResultVo.getBoatType()==null ? "lushang" : bestPartnerResultVo.getBoatType();
            BestPartnerResultVo vo = temp.get(key);
            if(vo == null || vo.getAchievement().compareTo(bestPartnerResultVo.getAchievement())>0){
                vo = bestPartnerResultVo;
                temp.put(key,vo);
            }
        }
        List<BestPartnerResultVo> ret = new ArrayList<>();
        for(String key : temp.keySet()){
            ret.add(temp.get(key));
        }
        return ret;
    }

    @Override
    public List<BestPartnerResultVo> getBestWeekList(Map<String, Object> map) {
        return sportsmanMapper.getBestWeekList(map);
    }

    @Override
    public List<BestPartnerCountVo> getBestPartnerCountVoList(int sportsManId) {
        return sportsmanMapper.getBestPartnerCountVoList(sportsManId);
    }

    @Override
    public String getNameById(int sportsManId) {
        return sportsmanMapper.getNameById(sportsManId);
    }

    @Override
    public List<InjuryCountVo> injuryCount(Map<String, Object> map) {
        Set<InjuryCountVo> set = init();
        List<InjuryCountVo> listdb = sportsmanMapper.injuryCount(map);
        for(InjuryCountVo vo : listdb){
            if(set.contains(vo)){
                set.remove(vo);
            }
            set.add(vo);
        }
        return new ArrayList<> (set);
    }

    @Override
    public List<InjuryCountVo> injuryHoursCount(Map<String, Object> map) {
        Set<InjuryCountVo> set = init();
        List<InjuryCountVo> listdb = sportsmanMapper.injuryCount(map);
        for(InjuryCountVo vo : listdb){
            if(set.contains(vo)){
                set.remove(vo);
            }
            set.add(vo);
        }
        return new ArrayList<> (set);
    }

    private Set<InjuryCountVo> init(){
        Set<InjuryCountVo> list = new HashSet<>();
        InjuryCountVo vo1 = new InjuryCountVo();
        vo1.setReason("上肢");
        vo1.setCount(0);
        vo1.setStartTime(new Date());
        list.add(vo1);
        InjuryCountVo vo2 = new InjuryCountVo();
        vo2.setReason("下肢");
        vo2.setCount(0);
        vo2.setStartTime(new Date());
        list.add(vo2);
        InjuryCountVo vo3 = new InjuryCountVo();
        vo3.setReason("躯干");
        vo3.setCount(0);
        vo3.setStartTime(new Date());
        list.add(vo3);
        InjuryCountVo vo4 = new InjuryCountVo();
        vo4.setReason("颈椎");
        vo4.setCount(0);
        vo4.setStartTime(new Date());
        list.add(vo4);
        return list;
    }

    @Override
    public List<BestPartnerResultVo> getBestResultPK(SportManBestResultVo sportManBestResultVo) {
        return sportsmanMapper.getBestResultPK(sportManBestResultVo);
    }

    @Override
    public List<OarTypeVo> getCountPulpPosition(int sportsManId) {
        return sportsmanMapper.getCountPulpPosition(sportsManId);
    }

    @Override
    public List<Map<String, Object>> selectSportsmanCountByType() {
        return sportsmanMapper.selectSportsmanCountByType();
    }

    @Override
    public List<Integer> selectSportsmanIdListBySex(int sex) {
        List<Integer> idList = new ArrayList<>();
        Sportsman sportsman = new Sportsman();
        sportsman.setSex(sex);
        List<Sportsman> list = sportsmanMapper.selectSportsmanList(sportsman);
        list.forEach( p->{
            idList.add(p.getId());
        });
        return idList;
    }

    @Override
    public Sportsman qrySportsmanByName(String sportsmanName) {
        Sportsman sportsman = new Sportsman();
        sportsman.setName(sportsmanName);
        List<Sportsman> list = sportsmanMapper.selectSportsmanList(sportsman);
        if (list!=null){
            for(Sportsman sportsman1 : list){
                if(sportsman1.getName().equals(sportsmanName)){
                    return sportsman1;
                }
            }
        }
        return null;
    }
}